package com.awenhui.demo;

import com.awenhui.demo.filter.MyFilter2;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * 启动类
 */
@SpringBootApplication(scanBasePackages ={ "com.awenhui.*"})
@MapperScan({"com.awenhui.demo.dao","com.awenhui.demo.module.samsql.dao"})
@ServletComponentScan
@EnableCaching
@EnableAsync
@EnableScheduling
@EnableFeignClients
@Slf4j
public class DemoApplication {
    public static void main(final String[] args) throws UnknownHostException {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(DemoApplication.class, args);
        String[] names = applicationContext.getBeanDefinitionNames();
        for (String name : names) {
            // System.out.println(name);
        }
        printSysInfo(applicationContext);
    }


    /**
     * 打印系统关键信息
     *
     * @author zhouxuanhong
     * @date 2019-10-24 10:59
     */
    static void printSysInfo(ConfigurableApplicationContext application) throws UnknownHostException {
        Environment env = application.getEnvironment();
  //      String ip = InetAddress.getLocalHost().getHostAddress();
        String port = env.getProperty("server.port");
        String path = env.getProperty("server.servlet.context-path");
        String prefixUrl="\t\thttp://localhost:"+port+path+"/";
        String projectName = env.getProperty("spring.application.name");
        log.info("\n------------------------------------------------------------------------------"
                + "\n\tApplication " + projectName + " is running! Access URLs:"
                + "\n\tLocal:"+prefixUrl
                + "\n\tExternal: "+prefixUrl
                + "\n\tswagger-ui: "+prefixUrl+"doc.html\n\t" + "Doc: "+prefixUrl+"/swagger-ui.html"
                +"\n------------------------------------------------------------------------------");
    }

//    /**
//     * 设置跨域
//     *
//     * @return
//     */
//    @Bean
//    public CorsFilter corsFilter() {
//        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
//        final CorsConfiguration config = new CorsConfiguration();
//        config.setAllowCredentials(true);
//        config.addAllowedOrigin(CorsConfiguration.ALL);
//        config.addAllowedHeader(CorsConfiguration.ALL);
//        config.setMaxAge(18000L);
//        config.addAllowedMethod("OPTIONS");
//        config.addAllowedMethod("GET");
//        config.addAllowedMethod("POST");
//        source.registerCorsConfiguration("/**", config);
//        return new CorsFilter(source);
//    }

    @Bean
    public FilterRegistrationBean myFilter2()
    {
        FilterRegistrationBean bean=new FilterRegistrationBean(new MyFilter2());
        bean.addUrlPatterns("/*");
        return bean;
    }

    @Bean
    public ModelMapper modelMapper() {
        ModelMapper modelMapper = new ModelMapper();
        return modelMapper;
    }

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder){
        return builder.build();
    }
}
